#ifndef MXVEC3_INCLUDED
#define MXVEC3_INCLUDED
#if !defined(__GNUC__)
#  pragma once
#endif

/************************************************************************

  3D Vector class
  Copyright (C) 1998 Michael Garland.  See "COPYING.txt" for details.

 ************************************************************************/

#include <cstdlib>
#include <cmath>
#include <iostream>

// NOTE: The use of mx_real is deprecated, and the typedef will
//       be going away in the near future.
//
#ifdef MIX_REAL_FLOAT
typedef float mx_real;
#else
#define MIX_REAL_DOUBLE
typedef double mx_real;
#endif

// Some systems use HUGE_VAL instead of HUGE
#if !defined(HUGE) && defined(HUGE_VAL)
#define HUGE HUGE_VAL
#endif

// Handle platforms, such as Win32, which don't define M_PI in <math.h>
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279502884197169399375105820974944592308
#endif

#ifndef FEQ_EPS
#define FEQ_EPS 1e-6
#define FEQ_EPS2 1e-12
#endif
inline bool FEQ(double a,double b,double eps=FEQ_EPS) { return fabs(a-b)<eps; }
inline bool FEQ2(double a,double b,double eps=FEQ_EPS2) {return fabs(a-b)<eps;}
#ifdef MIX_HAVE_FABSF
inline bool FEQ(float a,float b,float eps=FEQ_EPS) { return fabsf(a-b)<eps; }
inline bool FEQ2(float a,float b,float eps=FEQ_EPS2) { return fabsf(a-b)<eps; }
#endif

#if defined(WIN32) || !defined(MIX_HAVE_RINT)
//inline double rint(double x) { return floor(x + 0.5); }
#endif

#ifdef MIX_HAVE_RANDOM
inline double random1() { return (double)random() / (double)LONG_MAX; }
inline char   random_byte() { return (char)(random() & 0xff); }
#else
inline double random1() { return (double)rand() / (double)RAND_MAX; }
inline char   random_byte() { return (char)(rand() & 0xff); }
#endif

#ifndef MIX_NO_AXIS_NAMES
enum Axis {X=0, Y=1, Z=2, W=3};
#endif

/*******************************************************/
// stdmix.h

#ifndef _STDMIX_H_
#define _STDMIX_H_

#include <cassert>

#if SAFETY > 0
#  define AssertBound(t)  assert(t)
#  define PRECAUTION(x) x
#else
#  define AssertBound(t)
#  define PRECAUTION(x)
#endif

#endif

/*******************************************************/

class Vec3 {
private:
    double elt[3];

protected:
    inline void copy(const Vec3& v);

public:
    //
    // Standard constructors
    //
    Vec3(double x=0, double y=0, double z=0) { elt[0]=x; elt[1]=y; elt[2]=z; }
    Vec3(const Vec3& v) { copy(v); }
    Vec3(const float *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; }
    Vec3(const double *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; }

    //
    // Access methods
    //
    double& operator()(uint i)       { AssertBound(i<3); return elt[i]; }
    double  operator()(uint i) const { AssertBound(i<3); return elt[i]; }


//#ifdef __GNUC__
//    double& operator[](uint i)       { return elt[i]; }
//    double  operator[](uint i) const { return elt[i]; }
//#endif

    operator double*() { return elt; }
    operator const double*() const { return elt; }

    //
    // Comparison operators
    //
    inline bool operator==(const Vec3& v) const;
    inline bool operator!=(const Vec3& v) const;

    //
    // Assignment and in-place arithmetic methods
    //
    inline void set(double x,double y,double z) {elt[0]=x; elt[1]=y; elt[2]=z;}
    inline Vec3& operator=(const Vec3& v);
    inline Vec3& operator+=(const Vec3& v);
    inline Vec3& operator-=(const Vec3& v);
    inline Vec3& operator*=(double s);
    inline Vec3& operator/=(double s);

    //
    // Binary arithmetic methods
    //
    inline Vec3 operator+(const Vec3& v) const;
    inline Vec3 operator-(const Vec3& v) const;
    inline Vec3 operator-() const;

    inline Vec3 operator*(double s) const;
    inline Vec3 operator/(double s) const;
    inline double operator*(const Vec3& v) const;
    inline Vec3 operator^(const Vec3& v) const;

    ////////////////////////////////////////////////////////////////////////
    //
    // Primitive function definitions
    //

    inline double norm( void ) const
    {
        return sqrt(elt[0]*elt[0] + elt[1]*elt[1] + elt[2]*elt[2]);
    }

    inline double norm2( ) const
    {
        return elt[0]*elt[0] + elt[1]*elt[1] + elt[2]*elt[2];
    }

};

////////////////////////////////////////////////////////////////////////
//
// Method definitions
//

inline void Vec3::copy(const Vec3& v)
{
    elt[0]=v.elt[0]; elt[1]=v.elt[1]; elt[2]=v.elt[2];
}

inline bool Vec3::operator==(const Vec3& v) const
{
    double dx=elt[X]-v[X],  dy=elt[Y]-v[Y],  dz=elt[Z]-v[Z];
    return (dx*dx + dy*dy + dz*dz) < FEQ_EPS2;
}

inline bool Vec3::operator!=(const Vec3& v) const
{
    double dx=elt[X]-v[X],  dy=elt[Y]-v[Y],  dz=elt[Z]-v[Z];
    return (dx*dx + dy*dy + dz*dz) > FEQ_EPS2;
}

inline Vec3& Vec3::operator=(const Vec3& v)
{
    copy(v);
    return *this;
}

inline Vec3& Vec3::operator+=(const Vec3& v)
{
    elt[0] += v[0];   elt[1] += v[1];   elt[2] += v[2];
    return *this;
}

inline Vec3& Vec3::operator-=(const Vec3& v)
{
    elt[0] -= v[0];   elt[1] -= v[1];   elt[2] -= v[2];
    return *this;
}

inline Vec3& Vec3::operator*=(double s)
{
    elt[0] *= s;   elt[1] *= s;   elt[2] *= s;
    return *this;
}

inline Vec3& Vec3::operator/=(double s)
{
    elt[0] /= s;   elt[1] /= s;   elt[2] /= s;
    return *this;
}


inline Vec3 Vec3::operator+(const Vec3& v) const
{
    return Vec3(elt[0]+v[0], elt[1]+v[1], elt[2]+v[2]);
}

inline Vec3 Vec3::operator-(const Vec3& v) const
{
    return Vec3(elt[0]-v[0], elt[1]-v[1], elt[2]-v[2]);
}

inline Vec3 Vec3::operator-() const
{
    return Vec3(-elt[0], -elt[1], -elt[2]);
}

inline Vec3 Vec3::operator*(double s) const
{
    return Vec3(elt[0]*s, elt[1]*s, elt[2]*s);
}

inline Vec3 Vec3::operator/(double s) const
{
    return Vec3(elt[0]/s, elt[1]/s, elt[2]/s);
}

inline double Vec3::operator*(const Vec3& v) const
{
    return elt[0]*v[0] + elt[1]*v[1] + elt[2]*v[2];
}

inline Vec3 Vec3::operator^(const Vec3& v) const
{
    Vec3 w( elt[1]*v[2] - v[1]*elt[2],
	   -elt[0]*v[2] + v[0]*elt[2],
	    elt[0]*v[1] - v[0]*elt[1] );
    return w;
}


// Make scalar multiplication commutative
inline Vec3 operator*(double s, const Vec3& v) { return v*s; }


////////////////////////////////////////////////////////////////////////
//
// Primitive function definitions
//

inline double norm(const Vec3& v)
{
    return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}

inline double norm2(const Vec3& v)
{
    return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
}

inline double unitize(Vec3& v)
{
    double l=norm2(v);
    if( l!=1.0 && l!=0.0 )
    {
	l = sqrt(l);
	v /= l;
    }
    return l;
}


////////////////////////////////////////////////////////////////////////
//
// Misc. function definitions
//

inline std::ostream& operator<<(std::ostream& out, const Vec3& v)
{
    return out << v[0] << " " << v[1] << " " << v[2];
}

inline std::istream& operator>>(std::istream& in, Vec3& v)
{
    return in >> v[0] >> v[1] >> v[2];
}

#ifdef MXGL_INCLUDED
inline void glV(const Vec3& v) { glVertex3d(v[X], v[Y], v[Z]); }
inline void glN(const Vec3& v) { glNormal3d(v[X], v[Y], v[Z]); }
inline void glC(const Vec3& v) { glColor3d(v[X], v[Y], v[Z]); }
#endif


// MXVEC3_INCLUDED
#endif
